package cn.mymall.web.controller.houtai.mall;

import cn.mymall.entity.Province;
import cn.mymall.entity.mall.ShippingRegionPrice;
import cn.mymall.entity.mall.ShippingTemplate;
import cn.mymall.service.IProvinceService;
import cn.mymall.service.mall.IShippingRegionPriceService;
import cn.mymall.service.mall.IShippingTemplateService;
import cn.mymall.support.ResultCode;
import cn.mymall.util.DateUtil;
import cn.mymall.web.controller.houtai.BaseHoutaiController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ShippingRegionPriceApiController extends BaseHoutaiController {
    @Autowired
    private IShippingTemplateService shippingTemplateService;
    @Autowired
    private IProvinceService provinceService;
    @Autowired
    private IShippingRegionPriceService shippingRegionPriceService;

    @RequestMapping("/api/shippingregionprice/list")
    public ResultCode subShippingTemplateList(Integer templateId) {
        ShippingTemplate shippingTemplate = shippingTemplateService.getById(templateId);
        List<Province> provinceList = provinceService.list();
        List<Map> resMapList = new ArrayList<>();
        for(Province province : provinceList){
            Map map = new HashMap();
            map.put("provinceId", province.getId());
            map.put("provinceName", province.getName());
            ShippingRegionPrice shippingRegionPrice = shippingRegionPriceService.getOne(new QueryWrapper<ShippingRegionPrice>()
                    .eq("template_id", templateId).eq("province_id", province.getId()));
            if (shippingRegionPrice!=null) {
                map.put("id", shippingRegionPrice.getId());
                map.put("price", shippingRegionPrice.getPrice());
                map.put("addTime", shippingRegionPrice.getAddTime());
            }
            resMapList.add(map);
        }
        Map map = new HashMap();
        map.put("shippingRegionPriceList", resMapList);
        map.put("shippingTemplate", shippingTemplate);
        return ResultCode.getSuccessReturn(map);
    }

    @RequestMapping("/api/shippingregionprice/save")
    public ResultCode save(Integer templateId, Integer provinceId, BigDecimal price) {
        ShippingRegionPrice obj = shippingRegionPriceService.getOne(new QueryWrapper<ShippingRegionPrice>()
                .eq("template_id", templateId).eq("province_id", provinceId));
        if (obj == null) {
            obj = new ShippingRegionPrice();
            obj.setTemplateId(templateId);
            obj.setProvinceId(provinceId);
            obj.setAddTime(DateUtil.getCurFullTimestamp());
        }
        obj.setPrice(price);
        shippingRegionPriceService.saveOrUpdate(obj);
        return ResultCode.SUCCESS;
    }

}
